package com.example.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.domian.dto.UserFormDTO;
import com.example.domian.po.Address;
import com.example.domian.po.Result;
import com.example.domian.po.User;
import com.example.domian.query.UserQuery;
import com.example.domian.vo.AddressVo;
import com.example.domian.vo.UserVO;
import com.example.service.AddressService;
import com.example.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: UserController
 * Package: com.example.controller
 * Description:
 *
 * @Author: yyx
 * @Create: 2025/7/14 - 16:30
 */
@Api(tags="用户管理接口")
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private AddressService addressService;
    @PostMapping
    @ApiOperation("新增用户")
    public Result saveUser(@RequestBody UserFormDTO userFormDTO){
        User user = BeanUtil.copyProperties(userFormDTO, User.class);
        userService.save(user);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除用户")
    public  Result removeById(@ApiParam("用户id") @PathVariable("id") Long userId){
      userService.removeById(userId);
      return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询用户")
    public  Result getById(@ApiParam("用户id") @PathVariable("id") Long userId){
        User user = userService.getById(userId);
        UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
        return Result.success(userVO);
    }

    @GetMapping("/address/{id}")
    @ApiOperation("根据id查询地址")
    public  Result getAddressById(@ApiParam("用户id") @PathVariable("id") Long id){
        Address address = addressService.getById(id);
      AddressVo addressVos = BeanUtil.copyProperties(address, AddressVo.class);
        return Result.success(addressVos);
    }
    @GetMapping
    @ApiOperation("根据id查询批量用户")
    public  Result getUserById(@ApiParam("用户id") @RequestParam("ids") List<Long> userId){
        List<User> users = userService.listByIds(userId);
        List<UserVO> userVO = BeanUtil.copyToList(users, UserVO.class);
        return Result.success(userVO);
    }
    @PutMapping("/{id}/deduction/{money}")
    @ApiOperation("根据id扣减余额")
    public  Result deductBalance(@PathVariable("id")Long id,@PathVariable("money") Integer money){
            userService.deductBalance(id,money);
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("复杂条件查询用户")
    public  Result queryUser(UserQuery userQuery){
        String username = userQuery.getName();
        Integer status = userQuery.getStatus();
        Integer minBalance = userQuery.getMinBalance();
        Integer maxBalance = userQuery.getMaxBalance();
        LambdaQueryWrapper<User> wrapper = new QueryWrapper<User>().lambda();
        wrapper
                .like(username!=null,User::getUsername,username)
                .eq(status!=null,User::getStatus,status)
                .ge(minBalance!=null,User::getBalance,minBalance)
                .le(maxBalance!=null,User::getBalance,maxBalance);
        List<User> users=userService.list(wrapper);
        List<UserVO> userVoList =BeanUtil.copyToList(users,UserVO.class);
        return Result.success(userVoList);

    }

    @GetMapping("/by/{id}")
    @ApiOperation("根据id查询用户接口对应的地址")
    public Result queryUserAndAddressById(@ApiParam("用户id") @PathVariable("id") Long userId){
   UserVO userVO= userService.queryUserAndAddressById(userId);
    return Result.success(userVO);
    }
    @GetMapping("/by")
    @ApiOperation("根据id批量查询用户接口对应的地址")
    public Result queryUserByIdList(@ApiParam("用户id集合") @RequestParam("ids") List<Long> ids){
        return userService.queryUserByIdList(ids);
    }
}
